zabbix监控进程的CPU和内存占用量

        需要对特定服务进行监控,于是,通过编写脚本获取各个进程占用系统资源的信息,从而使用zabbix采集到这些数据进行特定进程的基础监控。

        主要需要监控的程序如下:

        nginx、 redis、 mysql、 tomcat、 sentinel、 mongodb、 openfire、 kafka、 zookeeper、 twemproxy、 mycat

        首先,在agent端编写监控脚本,脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
[zabbix@localhost ~]$ cat zabbix-2.4.4/scripts/processstatus.sh
#!/bin/bash
#license:GPL
#mail:admin@huxianglin.cn
#date:2015.06.02
nginx(){
ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
nginxcpu(){
ps aux|grep "nginx"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
redis(){
ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
rediscpu(){
ps aux|grep "redis"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
mysql(){
ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
mysqlcpu(){
ps aux|grep "mysql"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
tomcat(){
ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
tomcatcpu(){
ps aux|grep "tomcat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
sentinel(){
ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
sentinelcpu(){
ps aux|grep "sentinel"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
mongodb(){
ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
mongodbcpu(){
ps aux|grep "mongod"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
openfire(){
ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
openfirecpu(){
ps aux|grep "openfire"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
kafka(){
ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
kafkacpu(){
ps aux|grep "kafka"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
zookeeper(){
ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
zookeepercpu(){
ps aux|grep "zookeeper"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
twemproxy(){
ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
twemproxycpu(){
ps aux|grep "twemproxy"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
mycat(){
ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$6}; END{print sum}'
}
mycatcpu(){
ps aux|grep "mycat"|grep -v "grep"|grep -v "processstatus.sh"|awk '{sum+=$3}; END{print sum}'
}
case "$1" in
nginx)
nginx
;;
nginxcpu)
nginxcpu
;;
redis)
redis
;;
rediscpu)
rediscpu
;;
mysql)
mysql
;;
mysqlcpu)
mysqlcpu
;;
tomcat)
tomcat
;;
tomcatcpu)
tomcatcpu
;;
sentinel)
sentinel
;;
sentinelcpu)
sentinelcpu
;;
mongodb)
mongodb
;;
mongodbcpu)
mongodbcpu
;;
openfire)
openfire
;;
openfirecpu)
openfirecpu
;;
kafka)
kafka
;;
kafkacpu)
kafkacpu
;;
zookeeper)
zookeeper
;;
zookeepercpu)
zookeepercpu
;;
twemproxy)
twemproxy
;;
twemproxycpu)
twemproxycpu
;;
mycat)
mycat
;;
mycatcpu)
mycatcpu
;;
*)
echo "Usage: $0 {nginx|nginxcpu|redis|rediscpu|mysql|mysqlcpu|tomcat|tomcatcpu|sentinel|sentinelcpu|mongodb|mongodbcpu|openfire|openfirecpu|kafka|kafkacpu|zookeeper|zookeepercpu|twemproxy|twemproxycpu|mycat|mycatcpu}"
;;
esac

        然后修改脚本的权限,使用:

1
chmod +x processstatus.sh

        在zabbix_agentd.con配置文件中末尾增加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[zabbix@localhost ~]$ tail -23 zabbix-2.4.4/etc/zabbix_agentd.conf
#monitor process
UserParameter=process.nginx.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh nginx
UserParameter=process.nginx.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh nginxcpu
UserParameter=process.redis.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh redis
UserParameter=process.redis.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh rediscpu
UserParameter=process.mysql.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mysql
UserParameter=process.mysql.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mysqlcpu
UserParameter=process.tomcat.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh tomcat
UserParameter=process.tomcat.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh tomcatcpu
UserParameter=process.sentinel.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh sentinel
UserParameter=process.sentinel.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh sentinelcpu
UserParameter=process.mongodb.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mongodb
UserParameter=process.mongodb.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mongodbcpu
UserParameter=process.openfire.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh openfire
UserParameter=process.openfire.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh openfirecpu
UserParameter=process.kafka.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh kafka
UserParameter=process.kafka.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh kafkacpu
UserParameter=process.zookeeper.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh zookeeper
UserParameter=process.zookeeper.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh zookeepercpu
UserParameter=process.twemproxy.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh twemproxy
UserParameter=process.twemproxy.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh twemproxycpu
UserParameter=process.mycat.memory,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mycat
UserParameter=process.mycat.cpu,/home/zabbix/zabbix-2.4.4/scripts/processstatus.sh mycatcpu

        最后重启zabbix_agentd服务

1
2
pkill zabbix
zabbix-2.4.4/sbin/zabbix_agentd

        然后在zabbix服务端使用zabbix_get看能否取到相应的数据,像下面这样就是成功获取到了数据。

1
2
[root@localhost zabbix-2.4.4]# bin/zabbix_get -s 172.16.1.20 -p 10050 -k process.nginx.memory
184876

        最后,需要在zabbix中定义模板。模板附件链接在下面。

        zabbix模板下载

        如果模板无法下载可以在附件中下载模板

        需要注明的是内存取到的值得单位是KB,所以定义item的时候使用自定义倍数乘以 1000,单位改成Byte,另外CPU占用率的值是带有小数点的一个数,所以在定义item的时 候需要定义值得类型是浮点型,并且该值是占用逻辑单核的CPU占用率,所以需要定义自 定义倍数,我实验中的服务器是2颗CPU,每颗CPU是8核16线程,所以自定义倍数是原来 的基础上除以32,单位改成%就好。

        下面是做好之后的显示效果: